package com.vanzo.talkie;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.bean.Channel;
import com.vanzo.talkie2.R;

/**
 * Created by tisen on 2018/1/23.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private TextView tfv, rfv, tx, rx, volume;
    private TextView work_text, gbw_text, power_text;
    private Switch work_switch, gbw_switch, power_switch;
    private ImageView back;
    private TextView save;
    private TextView more;

    private Channel channel;
    private ATHelper atHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        channel = application.getUsedChannel();
        atHelper = getAtHelper();
        findViewSetListener();
    }

    private void findViewSetListener() {
        tfv = (TextView) findViewById(R.id.setting_edit_tfv_edit);
        rfv = (TextView) findViewById(R.id.setting_edit_rfv_edit);
        tx = (TextView) findViewById(R.id.setting_edit_tx_edit);
        rx = (TextView) findViewById(R.id.setting_edit_rx_edit);
        volume = (TextView) findViewById(R.id.setting_edit_sq_edit);

        work_text = (TextView) findViewById(R.id.setting_work_mode_text);
        gbw_text = (TextView) findViewById(R.id.setting_gbw_mode_text);
        power_text = (TextView) findViewById(R.id.setting_power_mode_text);

        work_switch = (Switch) findViewById(R.id.setting_work_mode_switch);
        gbw_switch = (Switch) findViewById(R.id.setting_gbw_mode_switch);
        power_switch = (Switch) findViewById(R.id.setting_power_mode_switch);

        back = (ImageView) findViewById(R.id.setting_back_img);
        save = (TextView) findViewById(R.id.setting_top_save);
        more = (TextView) findViewById(R.id.setting_more_text);

        back.setOnClickListener(this);
        save.setOnClickListener(this);
        more.setOnClickListener(this);
        more.setVisibility(View.INVISIBLE);

//        new ModeSwitch(work_text, work_switch, R.string.setting_work_mode_value_1, R.string.setting_work_mode_value_2, application.isWork_mode());
        new ModeSwitch(gbw_text, gbw_switch, R.string.setting_gbw_mode_value_1, R.string.setting_gbw_mode_value_2, application.isGbw_mode()).setVisibility(View.INVISIBLE);
        new ModeSwitch(power_text, power_switch, R.string.setting_power_mode_value_1, R.string.setting_power_mode_value_2, application.isPower_mode()).setVisibility(View.INVISIBLE);
        new FrPicker(this, tfv, channel.getTfv());
        new FrPicker(this, rfv, channel.getRfv());
        new CxPicker(this, tx, channel.getTx()).setVisibility(View.INVISIBLE);
        new CxPicker(this, rx, channel.getRx()).setVisibility(View.INVISIBLE);
        new VolumPicker(this, volume, application.getVolume_level()).setPickerSubmit(new VolumPicker.PickerSubmit() {
            @Override
            public void submit(int i, String s) {
                application.setVolume_level(s);
                Toast.makeText(SettingActivity.this, "正在设置音量", Toast.LENGTH_SHORT).show();
                atHelper.setVolume(s, volCallback);
            }
        });

    }

    private ATHelper.Callback volCallback = new ATHelper.Callback() {
        @Override
        public void callback(String result) {
            if (result.contains("+DMOSETVOLUME:0")) {
                handler.sendEmptyMessage(SET_VOLUMR_SUCCESS);
            } else if (result.contains("+DMOSETVOLUME:1")) {
                handler.sendEmptyMessage(SET_VOLUMR_FAILED);
            }
        }
    };

    private static final int SET_VOLUMR_SUCCESS = 101;
    private static final int SET_VOLUMR_FAILED = 102;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SET_VOLUMR_SUCCESS:
                    Toast.makeText(SettingActivity.this, "音量设置成功", Toast.LENGTH_SHORT).show();
                    break;
                case SET_VOLUMR_FAILED:
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_back_img:
                finish();
                break;
            case R.id.setting_top_save:
//                application.setWork_mode(work_switch.isChecked());
                application.setGbw_mode(gbw_switch.isChecked());
                application.setPower_mode(power_switch.isChecked());
                channel.setTfv(tfv.getText().toString());
                channel.setRfv(rfv.getText().toString());
                channel.setTx(tx.getText().toString());
                channel.setRx(rx.getText().toString());
                channel.setSq(volume.getText().toString());
                if (channel.getId() == null) {
                    application.updateDefaultChannel(channel);
                } else {
                    application.updateChannel(channel);
                }
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);
                setResult(TalkieActivity.CHANNEL_CHANGE_RESULT_CODE);
                finish();
                break;
            case R.id.setting_more_text:
                Intent intent = new Intent();
                intent.setClass(this, MoreSetting.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
